package com.pignest.hotel.ui.room;

import java.util.Set;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;

import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.UIUtils;


@SuppressWarnings("unchecked")
public class RoomTypeListBox extends Listbox implements EventListener {

	private static final long serialVersionUID = -6004942514449988781L;
	
	public RoomTypeListBox() {
//		addEventListener("onSelect", this);
	}

	public boolean removeSelected() {
		if (getSelectedCount() == 0)
			return false;
		Set<Listitem> selected = getSelectedItems();
		RoomService roomService = Services.getRoomService();
		for (Listitem item : selected) {
			if (roomService.hasRooms((String)item.getAttribute("roomTypeId")))
				return false;
			System.out.println(item.getAttribute("roomTypeId"));
		}
		for (Listitem item : selected) {
			roomService.removeRoomType(roomService.getRoomType((String)item.getAttribute("roomTypeId")));
		}
		return true;
	}
	
	
	public void removeAll() {
		int count = getItemCount();
		for (int i = count - 1; i >= 0; i--) {
			removeItemAt(i);
		}
	}

	@Override
	public void onEvent(Event event) throws Exception {
		System.out.println(event.getName());
	}
	
	public String getSelectedItemId() {
		Set<Listitem> selected = getSelectedItems();
		if (selected == null || selected.size() != 1) {
			UIUtils.alert("请选择一条记录!");
			return null;
		}
		Listitem item = selected.iterator().next();
		return (String)item.getAttribute("roomTypeId");
	}
}
